package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractXYmyUnionReq;
import com.game.server.message.proto.out.XYclubinfoStruct;
import com.game.server.message.proto.out.XYmyUnionRsp;
import com.game.server.message.proto.out.XYunioninfoStruct;

public class XYmyUnionReq extends AbstractXYmyUnionReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYmyUnionRsp rsp = new XYmyUnionRsp();
            XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(this.unionid);
            if (unionInfo!=null){
                XyClubInfo xyClubInfo = DataManager.getInstance().getMapClub().get(this.clubid);

                XYunioninfoStruct unioninfoStruct = new XYunioninfoStruct();
                unioninfoStruct.setId(unionInfo.getId());;
                unioninfoStruct.setName(unionInfo.getUnion_name());
                unioninfoStruct.setHead(unionInfo.getUnion_head());
                unioninfoStruct.setMemberRatio(unionInfo.getRatio());
                unioninfoStruct.setCreatorId(unionInfo.getCreator_clubid());
                XyClubInfo createClub = DataManager.getInstance().getMapClub().get(unionInfo.getCreator_clubid());
                if (createClub!=null){
                    unioninfoStruct.setCreatorName(createClub.getName());
                }
                rsp.setUnioninfo(unioninfoStruct);
                XYclubinfoStruct[] clubinfoStructs = null;
                if (xyClubInfo!=null){
                    XyClubMemberInfo memberInfo = xyClubInfo.getPlayer().get(client.getUserId());
                    if (memberInfo!=null && (memberInfo.getIsManager()==1 || memberInfo.getIsManager()==2)){
                        if (xyClubInfo.getId() == unionInfo.getCreator_clubid()){
                            clubinfoStructs = new XYclubinfoStruct[unionInfo.getClubids().size()];
                            int index = 0;
                            for (Integer clubid : unionInfo.getClubids()) {
                                XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(clubid);
                                XYclubinfoStruct clubinfoStruct = new XYclubinfoStruct();
                                clubinfoStruct.setUnionedu(clubInfo.getUnionedu());
                                clubinfoStruct.setId(clubInfo.getId());
                                clubinfoStruct.setName(clubInfo.getName());
                                clubinfoStruct.setHeadurl(clubInfo.getHeadurl());
                                clubinfoStruct.setAllPeopleCount(clubInfo.getPlayerCount());
                                clubinfoStruct.setHasxinyuvaluecount(clubInfo.getNumberOfPeopleWithQuota());
                                clubinfoStruct.setPaijuCount(xyClubInfo.findAllTable(client.getUserId(),clubInfo.getId()).size());
                                clubinfoStruct.setUnionid(clubInfo.getUnionid());
                                clubinfoStructs[index] = clubinfoStruct;
                                index++;
                            }
                        }else{
                            clubinfoStructs = new XYclubinfoStruct[1];
                            XYclubinfoStruct clubinfoStruct = new XYclubinfoStruct();
                            clubinfoStruct.setUnionedu(xyClubInfo.getUnionedu());
                            clubinfoStruct.setId(xyClubInfo.getId());
                            clubinfoStruct.setName(xyClubInfo.getName());
                            clubinfoStruct.setHeadurl(xyClubInfo.getHeadurl());
                            clubinfoStruct.setAllPeopleCount(xyClubInfo.getPlayerCount());
                            clubinfoStruct.setHasxinyuvaluecount(xyClubInfo.getNumberOfPeopleWithQuota());
                            clubinfoStruct.setPaijuCount(xyClubInfo.findAllTable(client.getUserId(),xyClubInfo.getId()).size());//俱乐部牌局数
                            clubinfoStruct.setUnionid(xyClubInfo.getUnionid());
                            clubinfoStructs[0] = clubinfoStruct;
                        }
                    }
                }


                rsp.setClubinfos(clubinfoStructs);
            }
            client.Send(rsp);
        }
    }

}
